package com.example.demo.hcontroller;

import com.example.demo.pojo.Result;
import com.example.demo.pojo.Score;
import com.example.demo.service.IResultService;
import com.example.demo.service.IScoreService;
import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.List;

@Controller
@RequestMapping(value = "/hScore")
public class HScoreController {

    @Autowired
    private IScoreService ScoreService;


    @Resource
    private IResultService ResultService;

    @RequestMapping("/toScore")
    private String toScore(Model model, Score Score) {
        List<Score> list = ScoreService.selectScore();
        model.addAttribute("scorelist", list);
        List<Result> Resultlist=ResultService.selectResult();
        model.addAttribute("resultlist", Resultlist);
        return "score";
    }



    @RequestMapping("/getScore")
    public void selectScore(HttpServletResponse response, String openid, Integer tid) throws IOException {
        List<Score> list=ScoreService.selectScore();
        System.out.println("list:"+list.size());
        Gson gson = new Gson();
        String json = gson.toJson(list);
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Methods", "GET,POST");
        response.setContentType("text/json,charset=utf-8");
        response.setCharacterEncoding("utf-8");
        Writer out = response.getWriter();
        out.write(json);
        out.flush();
    }

    @RequestMapping("/deleteScore")
    private String deleteScore(Model model, Score Score, Integer id) {
        ScoreService.deleteByPrimaryKey(id);
        return "redirect:toScore";
    }
}
